home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 8 / Night Owl CD-ROM (NOPV8) (Night Owl Publisher) (1993).ISO / 017a / binutils.arj / NM.C < prev    next >
C/C++ Source or Header  |  1991-03-27  |  28KB  |  1,196 lines

  1. /* Describe symbol table of a rel file.
  2.    Copyright (C) 1986, 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <ar.h>
  20. #include <sys/types.h>
  21. #include <sys/file.h>
  22. #include "getopt.h"
  23.  
  24. #if !defined(A_OUT) && !defined(MACH_O)
  25. #define A_OUT
  26. #endif
  27.  
  28. #ifdef A_OUT
  29. #ifdef COFF_ENCAPSULATE
  30. #ifdef GNUDOS
  31. #include "aoutencap.h"
  32. #else
  33. #include "a.out.encap.h"
  34. #endif
  35. #else
  36. /* On native BSD systems, use the system's own a.out.h.  */
  37. #ifdef GNUDOS
  38. #include <aout.h>
  39. #else
  40. #include <a.out.h>
  41. #endif
  42. #endif
  43. #endif
  44.  
  45. #ifdef MACH_O
  46. #ifndef A_OUT
  47. #include <nlist.h>
  48. #ifndef N_TEXT
  49. #define N_TEXT 4
  50. #define N_DATA 6
  51. #define N_BSS 8
  52. #endif
  53. #ifndef N_FN
  54. #define N_FN 15
  55. #endif
  56. #endif
  57. #include <sys/loader.h>
  58. #endif
  59.  
  60. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  61. #include "stab.h"
  62.  
  63. /* Struct or union for header of object file.  */
  64.  
  65. #ifdef USG
  66. #include <string.h>
  67. /* You might need to compile with -I/usr/include/sys if your fcntl.h
  68.    isn't in /usr/include (which is where it should be according to POSIX).  */
  69. #include <fcntl.h>
  70. #else
  71. #include <strings.h>
  72. #endif
  73.  
  74. /* Alloca definitions and includes...  */
  75.  
  76. /* If compiled with GNU C, use the built-in alloca */
  77. #ifdef __GNUC__
  78. #define alloca __builtin_alloca
  79. #else
  80. /* If on a sun 4, make sure to include the right definition.  */
  81. #if defined(sun) && defined(sparc)
  82. #include "alloca.h"
  83. #else
  84. char *alloca ();
  85. #endif
  86. #endif
  87.  
  88. #ifndef GNUDOS
  89. char *malloc (), *realloc ();
  90. #endif
  91.  
  92. char *xmalloc (), *xrealloc ();
  93.  
  94. /* C++ demangler stuff.  */
  95. char *cplus_demangle ();
  96.  
  97. /* Print NAME on STREAM, demangling if necessary.  */
  98. void
  99. fprint_name (stream, name)
  100.      FILE *stream;
  101.      char *name;
  102. {
  103.   char *demangled = cplus_demangle (name);
  104.   if (demangled == NULL)
  105.     fputs (name, stream);
  106.   else
  107.     {
  108.       fputs (demangled, stream);
  109.       free (demangled);
  110.     }
  111. }
  112.  
  113. /* Special global symbol types understood by GNU LD.  */
  114.  
  115. /* The following type indicates the definition of a symbol as being
  116.    an indirect reference to another symbol.  The other symbol
  117.    appears as an undefined reference, immediately following this symbol.
  118.  
  119.    Indirection is asymmetrical.  The other symbol's value will be used
  120.    to satisfy requests for the indirect symbol, but not vice versa.
  121.    If the other symbol does not have a definition, libraries will
  122.    be searched to find a definition.  */
  123. #ifndef N_INDR
  124. #define N_INDR 0xa
  125. #endif
  126.  
  127. /* Warning message symbol.  The name of this symbol will be printed if
  128.    any symbols from it's file are required by the linker.  */
  129. #ifndef N_WARNING
  130. #define N_WARNING 0x1e
  131. #endif
  132.    
  133.  
  134. /* The following symbols refer to set elements.
  135.    All the N_SET[ATDB] symbols with the same name form one set.
  136.    Space is allocated for the set in the text section, and each set
  137.    element's value is stored into one word of the space.
  138.    The first word of the space is the length of the set (number of elements).
  139.  
  140.    The address of the set is made into an N_SETV symbol
  141.    whose name is the same as the name of the set.
  142.    This symbol acts like a N_DATA global symbol
  143.    in that it can satisfy undefined external references.  */
  144.  
  145. #ifndef N_SETA
  146. #define    N_SETA    0x14        /* Absolute set element symbol */
  147. #endif                /* This is input to LD, in a .o file.  */
  148.  
  149. #ifndef N_SETT
  150. #define    N_SETT    0x16        /* Text set element symbol */
  151. #endif                /* This is input to LD, in a .o file.  */
  152.  
  153. #ifndef N_SETD
  154. #define    N_SETD    0x18        /* Data set element symbol */
  155. #endif                /* This is input to LD, in a .o file.  */
  156.  
  157. #ifndef N_SETB
  158. #define    N_SETB    0x1A        /* Bss set element symbol */
  159. #endif                /* This is input to LD, in a .o file.  */
  160.  
  161. /* Macros dealing with the set element symbols defined in a.out.h */
  162. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  163. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  164.  
  165. #ifndef N_SETV
  166. #define N_SETV    0x1C        /* Pointer to set vector in text area.  */
  167. #endif                /* This is output from LD.  */
  168.  
  169. #ifndef __GNU_STAB__
  170.  
  171. /* Line number for the data section.  This is to be used to describe
  172.    the source location of a variable declaration.  */
  173. #ifndef N_DSLINE
  174. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  175. #endif
  176.  
  177. /* Line number for the bss section.  This is to be used to describe
  178.    the source location of a variable declaration.  */
  179. #ifndef N_BSLINE
  180. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  181. #endif
  182.  
  183. #endif /* not __GNU_STAB__ */
  184.  
  185. /* Name of this program.  */
  186.  
  187. char *program_name;
  188.  
  189. /* Number of input files specified.  */
  190.  
  191. int number_of_files;
  192.  
  193. /* Current file's name.  */
  194.  
  195. char *input_name;
  196.  
  197. /* Current member's name, or 0 if processing a non-library file.  */
  198.  
  199. char *input_member;
  200.  
  201. #ifdef MACH_O
  202. /* Section ordinals for N_SECT references.  */
  203. int text_section;
  204. int data_section;
  205. int bss_section;
  206. #endif
  207.  
  208. /* Offset within archive of the current member,
  209.    if we are processing an archive.  */
  210.  
  211. int member_offset;
  212.  
  213. /* Command options.  */
  214.  
  215. int external_only;    /* nonzero means print external symbols only.  */
  216. int sort_numerically;   /* sort in numerical order rather than alphabetic  */
  217. int reverse_sort;    /* sort in downward (alphabetic or numeric) order.  */
  218. int no_sort;        /* don't sort; print symbols in order in the file.  */
  219. int undefined_only;    /* print undefined symbols only.  */
  220. int file_on_each_line;    /* print file name on each line.  */
  221. int debugger_syms;    /* print the debugger-only symbols.  */
  222. int print_symdefs;    /* describe the __.SYMDEF data in any archive file specified.  */
  223.  
  224. /* The __.SYMDEF member of an archive has the following format:
  225.    1) A longword saying the size of the symdef data that follows
  226.    2) Zero or more  struct symdef  filling that many bytes
  227.    3) A longword saying how many bytes of strings follow
  228.    4) That many bytes of string data.
  229. */
  230.  
  231. struct symdef
  232.   {
  233.     long stringoffset;    /* Offset of this symbol's name in the string data */
  234.     long offset;    /* Offset in the archive of the header-data for the member that
  235.                defines this symbol.  */
  236.   };
  237.  
  238. /* Create a table of debugging stab-codes and corresponding names.  */
  239. #ifdef __GNU_STAB__
  240. #define __define_stab(NAME, CODE, STRING) {NAME, STRING},
  241. struct {enum __stab_debug_code code; char *string;} stab_names[]
  242.   = {
  243. #include "stab.def"
  244.     };
  245. #undef __define_stab
  246. #endif
  247.  
  248. void decode_switch ();
  249. int decode_arg ();
  250. void do_one_file (), do_one_rel_file (), do_symdef_member ();
  251. char *concat ();
  252.  
  253. main (argc, argv)
  254.      char **argv;
  255.      int argc;
  256. {
  257.   int i;
  258.   int c;
  259.   extern int optind;
  260.   int ind;
  261.   static struct option long_options[] = 
  262.     {
  263.       {"all",        0, &debugger_syms,    1},
  264.       {"extern-only",    0, &external_only,    1},
  265.       {"numeric-sort",    0, &sort_numerically,    1},
  266.       {"print-file-name", 0, &file_on_each_line,1},
  267.       {"no-sort",    0, &no_sort,        1},
  268.       {"reverse",    0, &reverse_sort,    1},
  269.       {"print-symdefs",    0, &print_symdefs,    1},
  270.       {"undefined-only",0, &undefined_only,    1},
  271.       {NULL, 0, NULL, 0}
  272.     };
  273.  
  274. #ifdef GNUDOS
  275.   _fmode = O_BINARY; /* set default file type */
  276. #endif
  277.  
  278.   program_name = argv[0];
  279.  
  280.   number_of_files = 0;
  281.   external_only = 0;
  282.   sort_numerically = 0;
  283.   reverse_sort = 0;
  284.   no_sort = 0;
  285.   undefined_only = 0;
  286.   file_on_each_line = 0;
  287.   debugger_syms = 0;
  288.   print_symdefs = 0;
  289.  
  290.   while ((c = getopt_long (argc, argv, "agnoprsu", long_options, &ind)) != EOF)
  291.     switch (c)
  292.       {
  293.       case 0:
  294.           break;
  295.     
  296.       case 'a':
  297.     debugger_syms = 1;
  298.     break;
  299.  
  300.       case 'g':
  301.     external_only = 1;
  302.     break;
  303.  
  304.       case 'n':
  305.     sort_numerically = 1;
  306.     break;
  307.  
  308.       case 'o':
  309.     file_on_each_line = 1;
  310.     break;
  311.  
  312.       case 'p':
  313.     no_sort = 1;
  314.     break;
  315.  
  316.       case 'r':
  317.     reverse_sort = 1;
  318.     break;
  319.  
  320.       case 's':
  321.     print_symdefs = 1;
  322.     break;
  323.  
  324.       case 'u':
  325.     undefined_only = 1;
  326.     break;
  327.  
  328.       default:
  329.     fprintf (stderr, "\
  330. Usage: %s [-agnoprsu] [+all] [+extern-only] [+numeric-sort]\n\
  331.        [+print-file-name] [+no-sort] [+reverse] [+print-symdefs]\n\
  332.        [+undefined-only] [file...]\n", program_name);
  333.     exit (1);
  334.     break;
  335.       }
  336.  
  337.   number_of_files = argc - optind;
  338.  
  339.   /* Now scan again and print the files.  */
  340.  
  341.   if (argc == optind)
  342.     do_one_file ("a.out");
  343.   else
  344.     for (i = optind; i < argc; i++)
  345.       do_one_file (argv[i]);
  346.   exit (0);
  347. }
  348.  
  349. /* Print the filename of the current file on 'outfile' (a stdio stream).  */
  350.  
  351. print_file_name (outfile)
  352.      FILE *outfile;
  353. {
  354.   fprintf (outfile, "%s", input_name);
  355.   if (input_member)
  356.     fprintf (outfile, "(%s)", input_member);
  357. }
  358.  
  359. /* process one input file */
  360. void scan_library ();
  361.  
  362. void
  363. do_one_file (name)
  364.      char *name;
  365. {
  366.   int len, desc, nchars;
  367.   char armag[SARMAG];
  368.  
  369.   desc = open (name, O_RDONLY, 0);
  370.  
  371.   if (desc < 0)
  372.     {
  373.       perror_name (name);
  374.       return;
  375.     }
  376.  
  377.   input_name = name;
  378.   input_member = 0;
  379.  
  380.   nchars = read (desc, armag, SARMAG);
  381.   if (nchars == SARMAG && !strncmp(armag, ARMAG, SARMAG))
  382.     scan_library (desc);
  383.   else
  384.     do_one_rel_file (desc, 0);
  385.  
  386.   close (desc);
  387. }
  388.  
  389. /* Read in the archive data about one member.
  390.    SUBFILE_OFFSET is the address within the archive of the start of that data.
  391.    The value returned is the length of the member's contents, which does
  392.    not include the archive data about the member.
  393.    A pointer to the member's name is stored into *MEMBER_NAME_PTR.
  394.  
  395.    If there are no more valid members, zero is returned.  */
  396.  
  397. int
  398. decode_library_subfile (desc, subfile_offset, member_name_ptr)
  399.      int desc;
  400.      int subfile_offset;
  401.      char **member_name_ptr;
  402. {
  403.   int bytes_read;
  404.   int namelen;
  405.   int member_length;
  406.   char *name;
  407.   struct ar_hdr hdr1;
  408.  
  409.   lseek (desc, subfile_offset, 0);
  410.  
  411.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  412.   if (!bytes_read)
  413.     ;        /* end of archive */
  414.  
  415.   else if (sizeof hdr1 != bytes_read)
  416.     error_with_file ("malformed library archive ");
  417.  
  418.   else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  419.     error_with_file ("malformatted header of archive member in ");
  420.  
  421.   else
  422.     {
  423.       for (namelen = 0; ; namelen++)
  424.     if (hdr1.ar_name[namelen] == 0 || hdr1.ar_name[namelen] == ' '
  425. #ifdef GNUDOS
  426.             )
  427. #else
  428.         /* Some systems use a slash?  Strange.  */
  429.         || hdr1.ar_name[namelen] == '/')
  430. #endif
  431.       break;
  432.  
  433.       name = (char *) xmalloc (namelen+1);
  434.       strncpy (name, hdr1.ar_name, namelen);
  435.       name[namelen] = 0;
  436.  
  437.       *member_name_ptr = name;
  438.  
  439.       return member_length;
  440.     }
  441.   return 0;   /* tell caller to exit loop */
  442. }
  443.  
  444. /* Scan a library and describe each member.  */
  445.  
  446. void
  447. scan_library (desc)
  448.      int desc;
  449. {
  450.   int this_subfile_offset = SARMAG;
  451.   int member_length;
  452.  
  453.   if (!file_on_each_line)
  454.     printf ("\n%s:\n", input_name);
  455.   
  456.   while (1)
  457.     {
  458.       member_length
  459.     = decode_library_subfile (desc, this_subfile_offset, &input_member);
  460.       if (member_length == 0)
  461.     break;
  462.  
  463.       /* Describe every member except the ranlib data if any.  */
  464.  
  465.       if (strcmp (input_member, "__.SYMDEF"))
  466.     do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
  467.       else if (print_symdefs)
  468.     do_symdef_member (desc, this_subfile_offset + sizeof (struct ar_hdr), member_length);
  469.  
  470.       this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
  471.     }
  472. }
  473.  
  474. /* Read a file's header and fill in various pieces of information.
  475.    Return 0 on failure.  */
  476.  
  477. int
  478. read_header_info (desc, offset, syms_offset, syms_size, strs_offset, strs_size)
  479.      int desc;
  480.      long int offset;
  481.      long int *syms_offset;
  482.      unsigned int *syms_size;
  483.      long int *strs_offset;
  484.      unsigned int *strs_size;
  485. {
  486.   int len;
  487.  
  488. #ifdef A_OUT
  489.   {
  490.     struct exec hdr;
  491.  
  492.     lseek (desc, offset, 0);
  493. #ifdef HEADER_SEEK_FD
  494.     /* Skip the headers that encapsulate our data in some other format
  495.        such as COFF.  */
  496.     HEADER_SEEK_FD (desc);
  497. #endif
  498.     len = read (desc, (char *) &hdr, sizeof (struct exec));
  499.     if (len == sizeof (struct exec) && !N_BADMAG (hdr))
  500.       {
  501.     *syms_offset = N_SYMOFF(hdr);
  502.     *syms_size = hdr.a_syms;
  503.     *strs_offset = N_STROFF(hdr);
  504.     lseek(desc, N_STROFF(hdr) + offset, 0);
  505.     if (read (desc, (char *) strs_size, sizeof *strs_size) != sizeof *strs_size)
  506.       {
  507.         error_with_file ("cannot read string table size in ");
  508.         return 0;
  509.       }
  510.     return 1;
  511.       }
  512.   }
  513. #endif
  514.  
  515. #ifdef MACH_O
  516.   {
  517.     struct mach_header mach_header;
  518.     char *hdrbuf;
  519.     struct load_command *load_command;
  520.     struct segment_command *segment_command;
  521.     struct section *section;
  522.     struct symtab_command *symtab_command;
  523.     int symtab_seen;
  524.     int len, cmd, seg, ordinal;
  525.  
  526.     symtab_seen = 0;
  527.  
  528.     lseek (desc, offset, 0);
  529.     len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
  530.     if (len == sizeof (struct mach_header) && mach_header.magic == MH_MAGIC)
  531.       {
  532.     hdrbuf = xmalloc (mach_header.sizeofcmds);
  533.     len = read (desc, hdrbuf, mach_header.sizeofcmds);
  534.     if (len != mach_header.sizeofcmds)
  535.       {
  536.         error_with_file ("failure reading Mach-O load commands in ");
  537.         return 0;
  538.       }
  539.     load_command = (struct load_command *) hdrbuf;
  540.     ordinal = 1;
  541.     for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  542.       {
  543.         switch (load_command->cmd)
  544.           {
  545.           case LC_SEGMENT:
  546.         segment_command = (struct segment_command *) load_command;
  547.         section = (struct section *) ((char *) (segment_command + 1));
  548.         for (seg = 0; seg < segment_command->nsects; ++seg, ++section, ++ordinal)
  549.           {
  550.             if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
  551.               text_section = ordinal;
  552.             else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
  553.               data_section = ordinal;
  554.             else if (!strncmp(SECT_BSS, section->sectname, sizeof section->sectname))
  555.               bss_section = ordinal;
  556.           }
  557.         break;
  558.           case LC_SYMTAB:
  559.         if (symtab_seen)
  560.           error_with_file ("more than one LC_SYMTAB in ");
  561.         else
  562.           {
  563.             symtab_seen = 1;
  564.             symtab_command = (struct symtab_command *) load_command;
  565.             *syms_offset = symtab_command->symoff;
  566.             *syms_size = symtab_command->nsyms * sizeof (struct nlist);
  567.             *strs_offset = symtab_command->stroff;
  568.             *strs_size = symtab_command->strsize;
  569.           }
  570.         break;
  571.           }
  572.         load_command = (struct load_command *)
  573.           ((char *) load_command + load_command->cmdsize);
  574.       }
  575.     free (hdrbuf);
  576.     return 1;
  577.       }
  578.   }
  579. #endif
  580.  
  581.   return 0;
  582. }
  583.  
  584. void print_symbols (), print_one_symbol ();
  585. void read_header ();
  586. int alphacompare (), valuecompare ();
  587. int filter_symbols ();
  588.  
  589. void
  590. do_one_rel_file (desc, offset)
  591.      int desc;
  592.      int offset;
  593. {
  594.   struct nlist *symbols_and_strings;
  595.   int symcount;
  596.   int totalsize;
  597.   char *strings;
  598.   long int syms_offset, strs_offset;
  599.   unsigned int syms_size, strs_size;
  600.  
  601.   if (!read_header_info (desc, offset, &syms_offset, &syms_size, &strs_offset, &strs_size))
  602.     {
  603.       error_with_file ("malformed input (not a rel file or archive) in ");
  604.       return;
  605.     }
  606.  
  607.   /* Number of symbol entries in the file.  */
  608.   symcount = syms_size / sizeof (struct nlist);
  609.  
  610.   totalsize = strs_size + syms_size;
  611.  
  612.   /* Allocate space for symbol entries and string table.  */
  613.   symbols_and_strings = (struct nlist *) xmalloc (totalsize);
  614.   strings = (char *) symbols_and_strings + syms_size;
  615.  
  616.   /* Read them both in.  */
  617.   lseek (desc, syms_offset + offset, 0);
  618.   if (syms_size != read (desc, (char *) symbols_and_strings, syms_size))
  619.     {
  620.       error_with_file ("premature end of file in symbols of ");
  621.       return;
  622.     }
  623.   lseek (desc, strs_offset + offset, 0);
  624.   if (strs_size != read (desc, (char *) strings, strs_size))
  625.     {
  626.       error_with_file ("premature end of file in strings of ");
  627.       return;
  628.     }
  629.  
  630.   /* Identify this file, if desired.  */
  631.  
  632.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  633.     printf ("\n%s:\n", input_member ? input_member : input_name);
  634.  
  635.   /* Discard the symbols we don't want to print; compact the rest down.  */
  636.  
  637.   symcount = filter_symbols (symbols_and_strings, symcount, strings);
  638.     
  639.   /* Modify each symbol entry to point directly at the symbol name.
  640.      This is so the sort routine does not need to be passed
  641.      the value of `strings' separately.  */
  642.  
  643.   {
  644.     struct nlist *p = symbols_and_strings;
  645.     struct nlist *end = symbols_and_strings + symcount;
  646.  
  647.     for (; p < end; p++)
  648.       {
  649.     /* A zero index means there is no string.  */
  650.     if (p->n_un.n_strx != 0)
  651.       {
  652.         if (p->n_un.n_strx > 0 && p->n_un.n_strx < strs_size)
  653.           p->n_un.n_name = strings + p->n_un.n_strx;
  654.         else
  655.           {
  656.         error_with_file ("bad string table offset in ");
  657.         return;
  658.           }
  659.       }
  660.       }
  661.   }
  662.  
  663.   /* Sort the symbols if desired.  */
  664.  
  665.   if (!no_sort)
  666.     qsort (symbols_and_strings, symcount, sizeof (struct nlist),
  667.        sort_numerically ? valuecompare : alphacompare);
  668.  
  669.   /* Print the symbols in the order they are now in.  */
  670.  
  671.   print_symbols (symbols_and_strings, symcount);
  672.  
  673.   free (symbols_and_strings);
  674. }
  675.  
  676. /* Choose which symbol entries to print;
  677.    compact them downward to get rid of the rest.
  678.    Return the number of symbols to be printed.  */
  679.  
  680. int
  681. filter_symbols (syms, symcount, strings)
  682.      struct nlist *syms;
  683.      int symcount;
  684.      char *strings;
  685. {
  686.   struct nlist *from = syms, *to = syms;
  687.   struct nlist *end = syms + symcount;
  688.  
  689.   while (from < end)
  690.     {
  691.       int keep = 0;
  692.  
  693.       /* undefined sym or common sym */
  694.       if (from->n_type == N_EXT) keep = !undefined_only || !from->n_value;
  695.       /* global defined sym */
  696.       else if (from->n_type & N_EXT) keep = !undefined_only;
  697.       /* debugger sym: normally don't print */
  698.       else if (from->n_type & ~(N_TYPE | N_EXT)) keep = debugger_syms;
  699.       /* local sym */
  700.       else keep = !external_only && !undefined_only;
  701.  
  702.       if (keep)
  703.     *to++ = *from;
  704.       from++;
  705.     }
  706.  
  707.   return to - syms;
  708. }
  709.  
  710. /* Comparison functions for sorting symbols.  */
  711.  
  712. int
  713. alphacompare (sym1, sym2)
  714.      struct nlist *sym1, *sym2;
  715. {
  716.   if (reverse_sort)
  717.     {
  718.       if (!sym2->n_un.n_name)
  719.     {
  720.       if (sym1->n_un.n_name) return -1;
  721.       else return 0;
  722.     }
  723.       if (!sym1->n_un.n_name) return 1;
  724.       return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
  725.     }
  726.   else
  727.     {
  728.       if (!sym1->n_un.n_name)
  729.     {
  730.       if (sym2->n_un.n_name) return -1;
  731.       else return 0;
  732.     }
  733.       if (!sym2->n_un.n_name) return 1;
  734.       return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
  735.     }
  736. }
  737.  
  738.  
  739. int
  740. valuecompare (sym1, sym2)
  741.      struct nlist *sym1, *sym2;
  742. {
  743.   if (reverse_sort)
  744.     return sym2->n_value - sym1->n_value;
  745.   else
  746.     return sym1->n_value - sym2->n_value;
  747. }
  748.  
  749. void
  750. print_symbols (syms, symcount)
  751.      struct nlist *syms;
  752.      int symcount;
  753. {
  754.   int i;
  755.  
  756.   for (i = 0; i < symcount; i++)
  757.     print_one_symbol (&syms[i]);
  758. }
  759.  
  760. void
  761. print_one_symbol (sym)
  762.      struct nlist *sym;
  763. {
  764.   if (file_on_each_line)
  765.     {
  766.       print_file_name (stdout);
  767.       printf (":");
  768.     }
  769.  
  770.   if (undefined_only)
  771.     {
  772.       if (sym->n_type == N_EXT && !sym->n_value)
  773.     {
  774.       fprint_name (stdout, sym->n_un.n_name);
  775.       printf ("\n");
  776.     }
  777.       return;
  778.     }
  779.  
  780.   if (sym->n_type & ~N_EXT || sym->n_value)
  781.     printf ("%08x ", sym->n_value);
  782.   else printf ("         ");
  783.  
  784.   switch (sym->n_type)
  785.     {
  786.       case N_EXT:
  787.         if (sym->n_value) printf ("C");
  788.         else printf ("U");
  789.     break;
  790.  
  791.       case 0:
  792.         if (sym->n_value) printf ("c");
  793.         else printf ("u");
  794.     break;
  795.  
  796.       case N_ABS | N_EXT:
  797.     printf ("A");
  798.     break;
  799.  
  800.       case N_ABS:
  801.     printf ("a");
  802.     break;
  803.  
  804.       case N_TEXT | N_EXT:
  805.     printf ("T");
  806.     break;
  807.  
  808.       case N_TEXT:
  809.     printf ("t");
  810.     break;
  811.  
  812.       case N_DATA | N_EXT:
  813.     printf ("D");
  814.     break;
  815.  
  816.       case N_DATA:
  817.     printf ("d");
  818.     break;
  819.  
  820.       case N_BSS | N_EXT:
  821.     printf ("B");
  822.     break;
  823.  
  824.       case N_BSS:
  825.     printf ("b");
  826.     break;
  827.  
  828.       case N_SETV | N_EXT:
  829.     printf ("V");
  830.     break;
  831.  
  832.       case N_SETV:
  833.     printf ("v");
  834.     break;
  835.  
  836.       case N_SETA | N_EXT:
  837.     printf ("L");
  838.     break;
  839.     
  840.       case N_SETA:
  841.     printf ("l");
  842.     break;
  843.     
  844.       case N_SETT | N_EXT:
  845.     printf ("X");
  846.     break;
  847.     
  848.       case N_SETT:
  849.     printf ("x");
  850.     break;
  851.     
  852.       case N_SETD | N_EXT:
  853.     printf ("Z");
  854.     break;
  855.     
  856.       case N_SETD:
  857.     printf ("z");
  858.     break;
  859.     
  860.       case N_SETB | N_EXT:
  861.     printf ("S");
  862.     break;
  863.     
  864.       case N_SETB:
  865.     printf ("s");
  866.     break;
  867.     
  868.       case N_INDR | N_EXT:
  869.     printf ("I");
  870.     break;
  871.     
  872.       case N_INDR:
  873.     printf ("i");
  874.     break;
  875.     
  876.       case N_WARNING | N_EXT:
  877.     printf ("W");
  878.     break;
  879.     
  880.       case N_WARNING:
  881.     printf ("w");
  882.     break;
  883.     
  884. #ifdef N_SECT
  885.       case N_SECT:
  886.     if (sym->n_sect == text_section)
  887.       printf ("t");
  888.     else if (sym->n_sect == data_section)
  889.       printf ("d");
  890.     else if (sym->n_sect == bss_section)
  891.       printf ("b");
  892.     else
  893.       printf ("%d", sym->n_sect);
  894.     break;
  895.  
  896.       case N_SECT | N_EXT:
  897.     if (sym->n_sect == text_section)
  898.       printf ("T");
  899.     else if (sym->n_sect == data_section)
  900.       printf ("D");
  901.     else if (sym->n_sect == bss_section)
  902.       printf ("B");
  903.     else
  904.       printf ("%d", sym->n_sect);
  905.     break;
  906. #endif
  907.  
  908.       default:
  909.     {
  910.       char *s;
  911.       int i;
  912. #ifdef __GNU_STAB__
  913.       s = "";
  914.       for (i = sizeof (stab_names) / sizeof (stab_names[0]) - 1;
  915.            i >= 0; i--)
  916.         {
  917.           if (stab_names[i].code
  918.           == (enum __stab_debug_code) sym->n_type)
  919.         {
  920.           s = stab_names[i].string;
  921.           break;
  922.         }
  923.         }
  924. #else /* not __GNU_STAB__ */
  925.       switch (sym->n_type)
  926.         {
  927.         case N_GSYM:
  928.           s = "GSYM";
  929.           break;
  930.         case N_FNAME:
  931.           s = "FNAME";
  932.           break;
  933.         case N_FUN:
  934.           s = "FUN";
  935.           break;
  936.         case N_STSYM:
  937.           s = "STSYM";
  938.           break;
  939.         case N_LCSYM:
  940.           s = "LCSYM";
  941.           break;
  942.         case N_RSYM:
  943.           s = "RSYM";
  944.           break;
  945.         case N_SLINE:
  946.           s = "SLINE";
  947.           break;
  948.         case N_DSLINE:
  949.           s = "DSLINE";
  950.           break;
  951.         case N_BSLINE:
  952.           s = "BSLINE";
  953.           break;
  954.         case N_SSYM:
  955.           s = "SSYM";
  956.           break;
  957.         case N_SO:
  958.           s = "SO";
  959.           break;
  960.         case N_LSYM:
  961.           s = "LSYM";
  962.           break;
  963.         case N_SOL:
  964.           s = "SOL";
  965.           break;
  966.         case N_PSYM:
  967.           s = "PSYM";
  968.           break;
  969.         case N_ENTRY:
  970.           s = "ENTRY";
  971.           break;
  972.         case N_LBRAC:
  973.           s = "LBRAC";
  974.           break;
  975.         case N_RBRAC:
  976.           s = "RBRAC";
  977.           break;
  978.         case N_BCOMM:
  979.           s = "BCOMM";
  980.           break;
  981.         case N_ECOMM:
  982.           s = "ECOMM";
  983.           break;
  984.         case N_ECOML:
  985.           s = "ECOML";
  986.           break;
  987.         case N_LENG:
  988.           s = "LENG";
  989.           break;
  990.         default:
  991.           s = "";
  992.         }
  993. #endif /* not __GNU_STAB__ */
  994.       /* %x treats them as unsigned anyway, so if we didn't cast
  995.          them we'd get 0xffffffff for all 1's instead of 0xff
  996.          or 0xffff.  */
  997.       printf ("- %02x %04x %5s",
  998. #ifdef N_SECT
  999.           (unsigned char) sym->n_sect,
  1000. #else
  1001.           (unsigned char) sym->n_other,
  1002. #endif
  1003.           (unsigned short) sym->n_desc, s);
  1004.     }
  1005.     }
  1006.  
  1007.   printf (" ");
  1008.   
  1009.   if (sym->n_un.n_name)
  1010.     fprint_name (stdout, sym->n_un.n_name);
  1011.   
  1012.   printf ("\n");
  1013. }
  1014.  
  1015. void
  1016. do_symdef_member (desc, offset, member_length)
  1017.      int desc;
  1018.      int offset;
  1019.      int member_length;
  1020. {
  1021.   int symdef_size;
  1022.   int nsymdefs;
  1023.   struct symdef *symdefs;
  1024.   int stringsize;
  1025.   char *strings;
  1026.   int i;
  1027.   char *member_name;
  1028.   int member_offset;
  1029.  
  1030.   /* read the string-table-length out of the file */
  1031.  
  1032.   lseek (desc, offset, 0);
  1033.   if (sizeof symdef_size != read (desc, &symdef_size, sizeof symdef_size))
  1034.     {
  1035.       error_with_file ("premature eof in ");
  1036.       return;
  1037.     }
  1038.  
  1039.   if (symdef_size < 0)
  1040.     {
  1041.       error_with_file ("invalid size value in ");
  1042.       return;
  1043.     }
  1044.  
  1045.   nsymdefs = symdef_size / sizeof (struct symdef);
  1046.   symdefs = (struct symdef *) alloca (symdef_size);
  1047.   if (symdef_size != read (desc, symdefs, symdef_size))
  1048.     {
  1049.       error_with_file ("premature eof in ");
  1050.       return;
  1051.     }
  1052.  
  1053.   if (stringsize < 0)
  1054.     {
  1055.       error_with_file ("invalid size value in ");
  1056.       return;
  1057.     }
  1058.  
  1059.   if (sizeof stringsize != read (desc, &stringsize, sizeof stringsize))
  1060.     {
  1061.       error_with_file ("premature eof in ");
  1062.       return;
  1063.     }
  1064.  
  1065.   strings = (char *) alloca (stringsize);
  1066.   if (stringsize != read (desc, strings, stringsize))
  1067.     {
  1068.       error_with_file ("premature eof in ");
  1069.       return;
  1070.     }
  1071.  
  1072.   if (stringsize + symdef_size + sizeof stringsize + sizeof symdef_size != member_length)
  1073.     {
  1074.       error_with_file ("size of data isn't what the data calls for in ");
  1075.       return;
  1076.     }
  1077.  
  1078.   if (!file_on_each_line && (number_of_files > 1 || input_member))
  1079.     printf ("\n%s:\n", input_member ? input_member : input_name);
  1080.     
  1081.   member_offset = -1;
  1082.   for (i = 0; i < nsymdefs; i++)
  1083.     {
  1084.       if (symdefs[i].stringoffset < 0 || symdefs[i].stringoffset >= stringsize)
  1085.     {
  1086.       error_with_file ("invalid entry in ");
  1087.       return;
  1088.     }
  1089.       if (member_offset != symdefs[i].offset)
  1090.     {
  1091.       member_offset = symdefs[i].offset;
  1092.       decode_library_subfile (desc, member_offset, &member_name);
  1093.     }
  1094.       if (file_on_each_line)
  1095.     {
  1096.       print_file_name (stdout);
  1097.       printf (":");
  1098.     }
  1099.       printf ("%s in %s\n", symdefs[i].stringoffset + strings, member_name);
  1100.     }
  1101. }
  1102.  
  1103. /* Report a fatal error.
  1104.    STRING is a printf format string and ARG is one arg for it.  */
  1105.  
  1106. fatal (string, arg)
  1107.      char *string, *arg;
  1108. {
  1109.   fprintf (stderr, "%s: ", program_name);
  1110.   fprintf (stderr, string, arg);
  1111.   fprintf (stderr, "\n");
  1112.   exit (1);
  1113. }
  1114.  
  1115. /* Report a nonfatal error.
  1116.    STRING is a printf format string and ARG is one arg for it.  */
  1117.  
  1118. error (string, arg)
  1119.      char *string, *arg;
  1120. {
  1121.   fprintf (stderr, "%s: ", program_name);
  1122.   fprintf (stderr, string, arg);
  1123.   fprintf (stderr, "\n");
  1124. }
  1125.  
  1126. /* Report a nonfatal error.
  1127.    STRING is printed, followed by the current file name.  */
  1128.  
  1129. error_with_file (string)
  1130.      char *string;
  1131. {
  1132.   fprintf (stderr, "%s: ", program_name);
  1133.   fprintf (stderr, string);
  1134.   print_file_name (stderr);
  1135.   fprintf (stderr, "\n");
  1136. }
  1137.  
  1138. /* Report a fatal error using the message for the last failed system call,
  1139.    followed by the string NAME.  */
  1140.  
  1141. perror_name (name)
  1142.      char *name;
  1143. {
  1144.   extern int errno, sys_nerr;
  1145.   extern char *sys_errlist[];
  1146.   char *s;
  1147.  
  1148.   if (errno < sys_nerr)
  1149.     s = concat ("", sys_errlist[errno], " for %s");
  1150.   else
  1151.     s = "cannot open %s";
  1152.   error (s, name);
  1153. }
  1154.  
  1155. /* Like malloc but get fatal error if memory is exhausted.  */
  1156.  
  1157. char *
  1158. xmalloc (size)
  1159.      int size;
  1160. {
  1161.   char *result = malloc (size);
  1162.   if (!result)
  1163.     fatal ("virtual memory exhausted", 0);
  1164.   return result;
  1165. }
  1166.  
  1167. /* Like realloc but get fatal error if out of memory.  */
  1168. char *
  1169. xrealloc (p, size)
  1170.      char *p;
  1171.      int size;
  1172. {
  1173.   char *result = realloc(p, size);
  1174.   if (!result)
  1175.     fatal ("virtual memory exhausted", 0);
  1176.   return result;
  1177. }
  1178.  
  1179. /* Return a newly-allocated string
  1180.    whose contents concatenate those of S1, S2, S3.  */
  1181.  
  1182. char *
  1183. concat (s1, s2, s3)
  1184.      char *s1, *s2, *s3;
  1185. {
  1186.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1187.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1188.  
  1189.   strcpy (result, s1);
  1190.   strcpy (result + len1, s2);
  1191.   strcpy (result + len1 + len2, s3);
  1192.   result[len1 + len2 + len3] = 0;
  1193.  
  1194.   return result;
  1195. }
  1196.